/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import edu.albany.csi518.sick.DiseaseSymptom;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author anthony
 */
public class SessionSearch extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        DiseaseSymptom disease_symptom = new DiseaseSymptom();
        String result_out = "";

        //Query query = new Query();
        boolean debugger = false;

        String search_val = (String) request.getParameter("search_val");
        String action = (String) request.getParameter("action");

        if (search_val == null) {
            search_val = "";
        }

        if (action == null) {
            action = "";
        }

        switch (action) {
            case "Search Diseases":
                disease_symptom.analyzeInput(search_val, debugger, true);
                break;
            case "Search Symptoms":
                disease_symptom.analyzeInput(search_val, debugger, false);
                break;
        }

        /* TODO output your page here. You may use following sample code. */
//            out.println("<!DOCTYPE html>");
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Disease search</title>");            
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet search at " + request.getContextPath() + "</h1>");
//            out.println("<br/>You pressed : " + action+" for: "+search_val + " debug: " + debugger);
        //out.println("<br/>"+disease_symptom.getLastQuery());
        Iterator it = disease_symptom.getIt();
        result_out += "<h2>Result</h2>";
        if (it.hasNext()) {
            while (it.hasNext()) {
                String key = (String) it.next();
                ArrayList<String> syms = disease_symptom.getVal(key);
                result_out += "<h3>" + key + "</h3>";
                String sym = "<p>";
                for (String s : syms) {
                    sym += "<a href=\"\">" + s + "</a>, ";
                    //out.println("<p>" + s + "</p>");
                }
                sym += "</p>";
                result_out += sym;
            }
            result_out += "</body>";
            result_out += "</html>";
        } else {
            result_out += "<p>No result was found for " + search_val;
        }

        session.setAttribute("result", result_out);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/SearchResults.jsp");
        dispatcher.forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
